package com.tongan.core.aop;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.util.FileCopyUtils;

public class GZipFilter implements Filter {
   private static final Logger logger = Logger.getLogger(GZipFilter.class);
   private static final String STATIC_TEMPLATE_SOURCE = "online/template";
   private static final String STATIC_TEMPLATE_SOURCE_2 = "clzcontext/template";
   private static final String STATIC_TEMPLATE_SOURCE_3 = "/content/";
   private static final String STATIC_TEMPLATE_SOURCE_4 = "/plug-in-ui/";
   private static final String STATIC_TEMPLATE_SOURCE_5 = "/template/";
   private static final String NO_FILTER = ".do";
   private static final String NO_FILTER_JSP = ".jsp";
   private static final String DIAN = ".";

   public void destroy() {
   }

   public void init(FilterConfig arg0) throws ServletException {
   }

   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      HttpServletResponse resp = (HttpServletResponse)response;
      HttpServletRequest req = (HttpServletRequest)request;
      String url = req.getRequestURI();
      String path = req.getContextPath();
      (new StringBuilder(String.valueOf(req.getScheme()))).append("://").append(req.getServerName()).append(":").append(req.getServerPort()).append(path).toString();
      if ((req.getRequestURI().indexOf("online/template") != -1 || req.getRequestURI().indexOf("clzcontext/template") != -1 || req.getRequestURI().indexOf("/content/") != -1 || req.getRequestURI().indexOf("/plug-in-ui/") != -1 || req.getRequestURI().indexOf("/template/") != -1) && req.getRequestURI().indexOf(".") != -1 && req.getRequestURI().indexOf(".do") == -1 && req.getRequestURI().indexOf(".jsp") == -1) {
         if (url.startsWith(req.getContextPath())) {
            url = url.replaceFirst(req.getContextPath(), "");
         }

         response.reset();
         String s = ResMime.get(url.substring(url.lastIndexOf(".")).replace(".", ""));
         if (s != null) {
            response.setContentType(s);
         }

         OutputStream os = null;
         InputStream is = null;

         try {
            is = this.getClass().getResourceAsStream(url);
            if (is != null) {
               this.cacheResource(request, response, chain);
               os = response.getOutputStream();
               FileCopyUtils.copy(is, os);
            } else {
               chain.doFilter(request, response);
            }
         } catch (IOException var25) {
            var25.printStackTrace();
         } finally {
            if (os != null) {
               try {
                  os.close();
               } catch (IOException var24) {
                  ;
               }
            }

            if (is != null) {
               try {
                  is.close();
               } catch (IOException var23) {
                  ;
               }
            }

         }
      } else {
         chain.doFilter(request, response);
      }

   }

   public void cacheResource(ServletRequest request, ServletResponse response, FilterChain chain) {
      HttpServletRequest req = (HttpServletRequest)request;
      HttpServletResponse res = (HttpServletResponse)response;
      String uri = req.getRequestURI();
      uri = uri.substring(uri.lastIndexOf(".") + 1);
      long date = 0L;
      if (uri.equalsIgnoreCase("jpg")) {
         date = System.currentTimeMillis() + 18000000L;
      }

      if (uri.equalsIgnoreCase("gif")) {
         date = System.currentTimeMillis() + 18000000L;
      }

      if (uri.equalsIgnoreCase("css")) {
         date = System.currentTimeMillis() + 18000000L;
      }

      if (uri.equalsIgnoreCase("js")) {
         date = System.currentTimeMillis() + 18000000L;
      }

      res.setDateHeader("Expires", date);
   }
}
